package programm;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import jniGlue.Librarylinker;
import java.awt.CardLayout;
import javax.swing.ImageIcon;
import java.awt.GridLayout;
import java.awt.Font;
import javax.swing.JTextArea;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;

public class V_startWindow extends JFrame implements ActionListener
{
	private JPanel contentPane;

	private Librarylinker fx5;

	JButton btnUseValueslider;
	JButton btnUseSound2Light;
	JPanel startPanel;
	JPanel valueSliderPanel;
	JPanel sound2LightPanel;
	private JTextArea disclaimer;
	private JMenuBar menuBar;
	private JMenuItem mntmExit;
	private JMenu mnMenu;
	private JMenuItem mntmBack;

	/**
	 * Create the panel.
	 */
	public V_startWindow(final Librarylinker currentos)
	{
		// Methode durch WindowBuilder Pro
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				fx5.closedmx(currentos.getUsbdmxPath()); // bei Windows muss der
															// String gesetzt
															// sein
			}
		});

		fx5 = currentos;

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 340);

		menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		mnMenu = new JMenu("Menu");
		menuBar.add(mnMenu);

		mntmBack = new JMenuItem("Back to Main Menu");
		mntmBack.addActionListener(this);
		mnMenu.add(mntmBack);

		mntmExit = new JMenuItem("Exit PJ-DMX");
		mntmExit.addActionListener(this);
		mnMenu.add(mntmExit);
		contentPane = new JPanel();
		setContentPane(contentPane);

		setTitle("PJ-DMX");
		contentPane.setLayout(new CardLayout(0, 0));

		startPanel = new JPanel();
		contentPane.add(startPanel, "startPanel");
		startPanel.setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		startPanel.add(panel_1, BorderLayout.NORTH);

		JLabel welcomeJLabel = new JLabel("Welcome to PJ-DMX");
		welcomeJLabel.setFont(new Font("Dialog", Font.BOLD, 16));
		panel_1.add(welcomeJLabel);

		disclaimer = new JTextArea();
		disclaimer
				.setText("PJ-DMX is an OpenSource (GPLv3) students project.\nIt is completely written in Java using existing (C-)Libraries \n     to communicate with the hardware.\n\nFeel free to use it, or even to contribute.\n\n# DMX-Slider is a Simple GUI where you can manually \n     control all DMX channels and their values.\n# Sound2Light plays MP3 files and creates a suitable \n     lightshow according to your songs.\n\nUse it --> Like it!");
		startPanel.add(disclaimer, BorderLayout.CENTER);
		disclaimer.setEditable(false);
		disclaimer.setFont(new Font("Dialog", Font.BOLD, 12));

		JPanel panel = new JPanel();
		startPanel.add(panel, BorderLayout.SOUTH);

		btnUseValueslider = new JButton("ValueSlider");
		btnUseValueslider.setIcon(new ImageIcon(V_startWindow.class
				.getResource("/Images/156px-HSStats.svg.png")));
		btnUseValueslider.addActionListener(this);
		panel.setLayout(new GridLayout(0, 2, 0, 0));
		panel.add(btnUseValueslider);

		btnUseSound2Light = new JButton("Sound2Light");
		btnUseSound2Light.setIcon(new ImageIcon(V_startWindow.class
				.getResource("/Images/Nuvola_apps_kmplot.png")));
		btnUseSound2Light.addActionListener(this);
		panel.add(btnUseSound2Light);
		btnUseSound2Light.addActionListener(this);
	}

	public void actionPerformed(ActionEvent event)
	{
		if (event.getSource() == btnUseValueslider)
		{
			CardLayout cl = (CardLayout) (contentPane.getLayout());

			valueSliderPanel = new V_dmxSlider(fx5);
			contentPane.add(valueSliderPanel, "valueSliderPanel");

			cl.show(contentPane, "valueSliderPanel");
		} 
		else if (event.getSource() == btnUseSound2Light)
		{
			CardLayout cl = (CardLayout) (contentPane.getLayout());

			sound2LightPanel = new V_audioPlayer(fx5);
			contentPane.add(sound2LightPanel, "sound2LightPanel");

			cl.show(contentPane, "sound2LightPanel");
		} 
		else if (event.getSource() == mntmBack)
		{
			CardLayout cl = (CardLayout) (contentPane.getLayout());

			sound2LightPanel = new V_audioPlayer(fx5);
			contentPane.removeAll();
			contentPane.add(startPanel, "startPanel");

			cl.show(contentPane, "startPanel");
		} 
		else if (event.getSource() == mntmExit)
		{
			this.dispose();
		}
	}
}
